const moment = require("moment/moment");
const fs = require("fs");
let tokenList = ["zhangsan","lisi"]
exports.isToken = (redirectUrl='/login')=>{
	return (req,res,next)=>{
		if(tokenList.includes(req.query.token)){
			next();
		}else{
			res.redirect(redirectUrl)
		}
	}
}
exports.createLog = function(myUrl){
	return (req,res,next)=>{
		const {url,method} = req;
		const log = {
			id:Math.random().toString(36).slice(2),
			url,
			method,
			ip:req.socket.remoteAddress,
			addTime:moment().format("YYYY-MM-DD hh:mm:ss")
		};
		fs.readFile(myUrl,"utf-8",(err,result)=>{
			fs.writeFile(myUrl,JSON.stringify([
				log,
				...JSON.parse(result)
			]),()=>{
				next();
			})
		})
	}
}